//
//  PlayViewController.m
//  BLDMusics
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "PlayViewController.h"
#import "THCircularProgressView.h"

@interface PlayViewController ()<PlayerToolDelegate>

@property (nonatomic, retain) UIImageView *picImgView;

@property (nonatomic, retain) THCircularProgressView *progressView;

@property (nonatomic, retain) NSTimer *progressTimer;

@end

@implementation PlayViewController

+ (instancetype)sharePlayViewBar
{
    static PlayViewController *tab = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tab = [[PlayViewController alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGTH - 60, SCREEN_WIDTH, 60)];
        tab.backgroundColor = [UIColor whiteColor];
    });
    return tab;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self =  [super initWithFrame:frame];
    if (self) {
        [self creatSubviews];
        
    }
    return self;
}



- (void)creatSubviews {
    
//    [DSYPlayerTool sharePlayerTool].delegate = self;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popPlayingView)];
    
    
    [self addGestureRecognizer:tap];
    
    
    self.progressView = [[THCircularProgressView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
    _progressView.lineWidth = 10.0f;
    _progressView.progressColor = [UIColor greenColor];
    [self addSubview:_progressView];
    
    self.picImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
    [self addSubview:_picImgView];
    _picImgView.backgroundColor = [UIColor redColor];
    _picImgView.clipsToBounds = YES;
    _picImgView.layer.cornerRadius = 20.0;


    
    self.listButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _listButton.frame = CGRectMake(SCREEN_WIDTH - 40, 15, 30, 30);
    [_listButton setBackgroundImage:[UIImage imageNamed:@"iconfont-musiclist"] forState:UIControlStateNormal];
    [self addSubview:_listButton];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _playButton.frame = CGRectMake(CGRectGetMinX(_listButton.frame) - 40, CGRectGetMinY(_listButton.frame), CGRectGetWidth(_listButton.frame), CGRectGetHeight(_listButton.frame));

    [_playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_playButton];
    
    
    self.songNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_picImgView.frame) + 5, CGRectGetMinY(_picImgView.frame), CGRectGetMinX(_playButton.frame) - CGRectGetMaxX(_picImgView.frame) - 5, CGRectGetHeight(_picImgView.frame) / 2)];
    [self addSubview:_songNameLabel];
    
    
    self.singerNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(_songNameLabel.frame), CGRectGetMaxY(_songNameLabel.frame), CGRectGetWidth(_songNameLabel.frame), CGRectGetHeight(_songNameLabel.frame))];
    [self addSubview:_singerNameLabel];
    
//    [tapView addSubview:_playButton];
    UISwipeGestureRecognizer *swipeToRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(lastSong)];
    swipeToRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self addGestureRecognizer:swipeToRight];
    
    UISwipeGestureRecognizer *swipeToLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextSong)];
    swipeToLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:swipeToLeft];
    
    self.progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.01
                                                          target:self
                                                        selector:@selector(timerFired:)
                                                        userInfo:nil
                                                         repeats:YES];
    
    [self updateView];
}


//////////////////////////////////////////////////////////////////////
//- (NSTimer *)progressTimer
//{
//    if (!_progressTimer) {
//        _progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.01
//                                                          target:self
//                                                        selector:@selector(timerFired:)
//                                                        userInfo:nil
//                                                         repeats:YES];
//    }
//    return _progressTimer;
//}


- (void)timerFired:(NSTimer *)timer
{
    CGFloat current = [DSYPlayerTool sharePlayerTool].current;
    CGFloat duration = [DSYPlayerTool sharePlayerTool].duration;
    
    self.progressView.percentage = current / duration;
}

- (void)playingWithTime:(NSTimeInterval)time
{
    
    self.progressView.percentage = time;
}

- (void)didStop
{
    
}
/////////////////////////////////////////////////////////////////////

- (void)lastSong {
    [[DSYMusicViewController sharedMusicPlayingVC] lastAction:nil];
}

- (void)nextSong {
    [[DSYMusicViewController sharedMusicPlayingVC] nextAction:nil];
}

- (void)playAction:(UIButton *)button {
    if ([DSYPlayerTool sharePlayerTool].isPlaying == YES) {
        [[DSYPlayerTool sharePlayerTool] pause];
        [self updateView];
    } else {
        [[DSYPlayerTool sharePlayerTool] play];
        [self updateView];
    }
}

- (void)updateView
{
    // 播放音乐列表中午音乐
    if ([DSYDataManager shareDataManager].allDataArray.count <= 0) {
        self.songNameLabel.text = @"您还没选择播放的歌曲";
        
        
        
    } else {
        self.currentIndex = [DSYMusicViewController sharedMusicPlayingVC].currentIndex;
        if ([DSYPlayerTool sharePlayerTool].isPlaying == YES) {
            [self.playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-pause"] forState:UIControlStateNormal];
        } else {
            [self.playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-play"] forState:UIControlStateNormal];
        }
        if (self.currentIndex > [DSYDataManager shareDataManager].allDataArray.count) {
            return;
        }
        
        DSYSongModel *music = [DSYDataManager shareDataManager].allDataArray[self.currentIndex];
        
        [self.picImgView sd_setImageWithURL:[NSURL URLWithString:music.picUrl]];
        self.songNameLabel.text = music.name;
        self.singerNameLabel.text = music.singerName;
        
    }
}


- (NSInteger)currentIndex
{
    return [DSYMusicViewController sharedMusicPlayingVC].currentIndex;
}

- (void)popPlayingView {

//    self.hidden = YES;
    [self.delegate popPlayingView];
}



@end
